<template>
  <div>
    <a-menu
        @click="handleClick"
        style="border-top: 1px solid #ebedf0;"
        v-model="current"
        :openKeys.sync="openKeys"
        mode="inline"
    >
      <a-menu-item v-for="menu in leftMenuTree" :key="menu.menuCode" :menu="menu">
        <a-icon :type="menu.icon"/>
        {{menu.menuName}}
      </a-menu-item>
    </a-menu>
  </div>
</template>

<script>
  import Vue from 'vue'
  import {SELECT_HEAD, SELECT_LEFT} from '../store/mutation-types'

  export default {
    name: 'LeftMenu',
    data () {
      return {
        current: ['org'],
        openKeys: ['org'],
        leftMenuTree: []
      }
    },
    watch: {
      openKeys (val) {
        console.log('openKeys', val)
      },
      '$store.state.userInfo' () {
        console.log('LeftMenu $store.state.userInfo', this.$store.state.userInfo)
        this.menuList = this.$store.state.userInfo.menuTree
        if (!Vue.ls.get(SELECT_HEAD)) {
          this.$store.state.selectHead = this.$store.state.userInfo.menuTree[0].menuCode
        } else {
          this.$store.state.selectHead = Vue.ls.get(SELECT_HEAD)
        }
      },
      '$store.state.selectHead' () {
        console.log('LeftMenu $store.state.selectHead', this.$store.state.selectHead)
        console.log('LeftMenu Vue.ls.get(SELECT_LEFT)', Vue.ls.get(SELECT_LEFT))
        this.$store.state.userInfo.menuTree.forEach(item => {
          if (this.$store.state.selectHead === item.menuCode) {
            this.leftMenuTree = item.children
            if (!Vue.ls.get(SELECT_LEFT)) {
              Vue.ls.set(SELECT_LEFT, item.children[0])
            }
            this.pushOrIframe()
          }
        })
      },
      'window.location.pathname' () {
        console.log('window.location.pathname', window.location.pathname)
        this.current = []
        this.current.push(window.location.pathname)
      }
    },
    methods: {
      handleClick (val, e) {
        Vue.ls.set(SELECT_LEFT, {
          menuCode: val.key,
          openType: val.item.$attrs.menu.openType,
          path: val.item.$attrs.menu.path
        })
        this.pushOrIframe()
      },
      pushOrIframe () {
        this.current = [Vue.ls.get(SELECT_LEFT).menuCode]
        if (Vue.ls.get(SELECT_LEFT).openType === 0) {
          this.$store.state.iframeSrc = null
          this.$router.push({name: Vue.ls.get(SELECT_LEFT).menuCode})
        } else {
          this.$store.state.iframeSrc = Vue.ls.get(SELECT_LEFT).path
          this.$router.push({name: 'iframe'})
        }
      },
      titleClick (e) {
        console.log('titleClick', e)
      }
    }
  }
</script>

<style scoped>

</style>
